<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/src/css/layui.css}">
</head>
<body>
<blockquote class="layui-elem-quote news_search">
    <form class="layui-form">
        <div style="margin-top: 1%">
            <a class="layui-btn layui-btn-normal typeAdd_btn" shiro:hasPermission="good:gt:delete">添加分类</a>
        </div>
    </form>
</blockquote>
    <script type="text/html" id="barEdit">
        <a class="layui-btn layui-btn-danger layui-btn-xs layui-btn-disabled" shiro:lacksPermission="good:gt:delete">删除</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del" shiro:hasPermission="good:gt:delete">删除</a>
    </script>

    <table id="goodType" lay-filter="test"></table>

    <script th:src="@{/src/layui.js}"></script>

    <script>
        layui.use(['table','jquery'],function () {
            var table = layui.table;
            var $ = layui.jquery;
            table.render({
                elem:'#goodType',
                width:500,
                url:'/goods/typeList',
                cols:[
                    [
                        {field:'goodsTypeName',title:'类型名称'},
                        {title:'操作',width:200,templet:'#barEdit'}
                    ]
                ],
                parseData:function (res) {
                    return {
                        "code":res.code==200?0:res.code,
                        "msg":res.message,
                        "data":res.data
                    }
                }

            })
            table.on('tool(test)',function (obj) {
                var data = obj.data;//行数据
                var event = obj.event;
                if(event === 'del'){
                    layer.confirm('删除该分类，将删除该分类下的所有商品！确认删除？',function (index) {
                        $.ajax({
                            url:'/goods/deleteType/'+data.goodsTypeId,
                            type:'delete',
                            dataType:'json',
                            success:function (res) {
                                if(res.code == 200){
                                    layer.msg('删除成功',{icon:1},function () {
                                        location.reload();
                                    })
                                }else{
                                    layer.msg('删除失败',{icon:5})
                                }
                            }
                        })

                    })
                }
            })
            //添加
            $(".typeAdd_btn").click(function () {
                var index = layer.open({
                    type:2,
                    title:'添加分类',
                    content:'/goods/addGoodType'
                });
                $(window).resize(function () {//窗口改变大小触发的事件
                    layer.full(index);
                })
                layer.full(index);
            })
        })
    </script>
</body>
</html>